<?php

/**
 * Created by PhpStorm.
 * User: kc
 * Date: 2017/3/2
 * Time: 10:38
 */
class AhpUserController extends BaseController{
    /**
     * @Author:      kc
     * @DateTime:   2017-03-02 11:15:05
     * @Description: 添加账号
     */
    public function createUser(){
        $validator = Validator::make(Input::all(),
            array(
                'token' =>  'required',
                'user_name' => 'required|unique:ahp_user,user_name',
                'real_name' => 'required',
                'phone' => 'required',
                'password' => 'required',
                //'confirm_password' => 'required',
            )
        );
        do{
            $messages = $validator->messages();
            //验证用户名唯一
            if($messages->has('user_name')) {
                $response = Lang::get('ahp_error.user_unique_error_3100');
                break;
            }
            if($validator->fails()){
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            try{
                $ahp_user = new AhpUsers();
                $ahp_user->user_name = Input::get('user_name');
                $ahp_user->real_name = Input::get('real_name');
                $ahp_user->phone = Input::get('phone');
              /*  if(Input::get('password') == Input::get('confirm_password')){
                    $ahp_user->password = Common::make(Input::get('password'));
                }else{
                    $response = Lang::get('ahp_error.user_password_differ_error_3104');
                    break;
                }*/
                $ahp_user->password = Common::make(Input::get('password'));
                $ahp_user->desc = Input::get('desc','');
                $ahp_user->status = 1;
                $ahp_user->save();
            }catch (Exception $e){
                Log::error("error", array("system" => "ahp" ,"event" => "createUser", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ahp_error.user_create_error_3101");
                break;
            }
            $response = array(
                'code' => 0,
                'msg' => 'ok',
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "createUser", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:   2017-03-02 10:41:17
     * @Description: 账号列表
     */
    public function userList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get("ahp_error.args_error_1000");
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            $ahp_users = AhpUsers::getUserList(Input::all());
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $ahp_users,
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "userList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:   2017-03-02 12:00:17
     * @Description: 修改账号状态
     */
    public function saveStatus(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'user_id' => 'required',
                'status' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get('ahp_error.args_error_1000');
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            $user_id = explode(",",Input::get('user_id'));
            try{
                foreach($user_id as $v){
                    $ahp_users = AhpUsers::find($v);
                    $ahp_users->status = Input::get('status');
                    $ahp_users->save();
                }
            }catch (Exception $e){
                Log::error("error", array("system" => "ahp" ,"event" => "createUser", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ahp_error.user_update_status_error_3102");
                break;
            }
            $response = array(
                'code' => 0,
                'msg' => 'ok',
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "saveStatus", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:   2017-03-02 14:00:00
     * @Description: 修改用户信息
     */
    public function updateUser(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'user_id' => 'required',
                'user_name' => 'unique:ahp_user,user_name,'.Input::get('user_id'),
            )
        );
        do{
            $messages = $validator->messages();
            //验证用户名唯一
            if($messages->has('user_name')) {
                $response = Lang::get('ahp_error.user_unique_error_3100');
                break;
            }
            if($validator->fails()){
                $response = Lang::get('ahp_error.args_error_1000');
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            try{
                $ahp_users = AhpUsers::find(Input::get('user_id'));
                $ahp_users->user_name = Input::get('user_name')?Input::get('user_name'):'';
                $ahp_users->real_name = Input::get('real_name')?Input::get('real_name'):'';
                if(Input::get('password') != $ahp_users->password){
                  /*  if(Input::get('password') == Input::get('confirm_password')){
                        $ahp_users->password = Common::make(Input::get('password'));
                    }else{
                        $response = Lang::get('ahp_error.user_password_differ_error_3104');
                        break;
                    }*/
                    $ahp_users->password = Common::make(Input::get('password'));
                }
                $ahp_users->phone = Input::get('phone')?Input::get('phone'):'';
                $ahp_users->desc = Input::get('desc')?Input::get('desc'):'';
                $ahp_users->updated_at = Common::currentTimeString();
                $ahp_users->save();
            }catch (Exception $e){
                Log::error("error", array("system" => "ahp" ,"event" => "createUser", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ahp_error.user_information_update_error_3103");
                break;
            }
            $response = array(
                'code' => 0,
                'msg' => 'ok',
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "updateUser", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /**
     * @Author:      kc
     * @DateTime:   2017-03-17 18:00:00
     * @Description: 账号详情
     */
    public function userDetails(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'user_id' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get('ahp_error.args_error_1000');
                $response["result"] = $validator->messages()->all();
                break;
            }
            $account_user = $this->verificationAhpUserToken(Input::get("token"),$response);
            if(is_null($account_user)){
                break;
            }
            $ahp_users = AhpUsers::getUserDetails(Input::get('user_id'));
            $response =  array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $ahp_users,
            );
        }while(false);
        Log::info("info", array("system" => "ahp" ,"event" => "userDetails", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}